from langchain_huggingface import HuggingFaceEmbeddings
from langchain.storage import LocalFileStore
from langchain.embeddings.cache import CacheBackedEmbeddings
import hashlib

# 使用本地模型路径
local_model_path = r"D:\soft\sentence-transformers-master\bge-large-zh-v1.5"

embeddings = HuggingFaceEmbeddings(
    model_name=local_model_path,
    model_kwargs={'device': 'cpu'},
    encode_kwargs={'normalize_embeddings': True}
)

# 缓存嵌入
fs = LocalFileStore("./cache/")
cached_embeddings = CacheBackedEmbeddings.from_bytes_store(
    embeddings,
    fs,
    key_encoder=lambda x: hashlib.sha256((embeddings.model_name + x.decode('utf-8')).encode('utf-8')).digest()
)

# 测试
print(list(fs.yield_keys()))